package 力扣._409_最长回文串;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int longestPalindrome(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i)))
                map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
            else
                map.put(s.charAt(i), 1);
        }
        int length = 0;
        boolean ishasodd = false;   //是否有奇数
        //每个字符的数量若为偶数则直接加，若为奇数，则将其减一再加上去，
        Set<Character> characters = map.keySet();
        for (Character character : characters) {
            if (map.get(character) % 2 == 0)
                length += map.get(character);
            else {
                length += map.get(character) - 1;
                ishasodd = true;
            }
        }
        if (ishasodd)
            length++;
        return length;
    }

    public static void main(String[] args) {
        int i = new Solution().longestPalindrome("abccccdd");
    }
}
